﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting {
    public class Shell {

        private string input = string.Empty;

        private string output = string.Empty;

        private ScriptingObject intp = null;

        private int count = 1;

        static void Main(string[] args) {
            Console.Title = "FunctionalCLR Shell";
            Console.WriteLine("Copyright 2009 Zove Development Team\n");

            ScriptingContext context = new ScriptingContext();
            ScriptingObject intp = null;
            Shell p = new Shell(intp);
            if (args.Length > 0) {
                List<string> programArgs = new List<string>();
                if (args.Length > 1) {
                    for (int index = 1; index < args.Length; index++) {
                        programArgs.Add(args[index]);
                    }
                }
                intp = new ScriptingObject(args[0], context);
                intp.Parameters = programArgs.ToArray();
                return;
            }

            p.StartSession();
            Console.WriteLine("Session ends");
        }

        public Shell(ScriptingObject intp) {
            this.intp = intp;
        }

        public void LoadSession(string fileName) {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            string input = string.Empty;
            while ((input = sr.ReadLine()) != null) {
                Console.WriteLine(
                    string.Format("in[{0}] = {1}", count, input));
                object output = intp.Process(input);
                Console.WriteLine(
                    string.Format("out[{0}] = {1}", count++, output));
            }

            sr.Close();
        }

        public void StartSession() {
            Console.Write("in[{0}] := ", count);
            while (true) {
                input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input)) {
                    if (!input.EndsWith(";")) {
                        input = input + ";";
                    }
                    if (input.Equals("exit;")) {
                        break;
                    }

                    try {
                        output = intp.Process(input).ToString();
                        Console.WriteLine(
                            string.Format("out[{0}] = {1}", count, output));
                    } catch (Exception ex) {
                        StreamWriter writer = File.AppendText(Utils.LogFile);
                        writer.WriteLine(ex.InnerException.Message + "\n" + ex.InnerException.StackTrace);

                        Console.WriteLine("Err[{0}] = {1}", count, ex.InnerException.Message);

                        writer.Close();
                    }
                }
                Console.Write(string.Format("in[{0}] := ", ++count));
            }
        }
    }
}
